<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>字符串解码</title>
</head>
<body>
  <script>
    function fn(s) {
      let numStack = [];              // 倍数 num 的等待栈
      let strStack = [];              // 待拼接 str 的等待栈
      let num = 0, result = '';
      for (let i = 0; i < s.length; i++) {
        const item = s[i];
        if (!isNaN(item)) {              // 判断是数字时
          num = num * 10 + parseInt(item);
        } else if (item === '[') {
          strStack.push(result);
          result = '';
          numStack.push(num);
          num = 0;
        } else if (item === ']') {
          const repeatTimes = numStack.pop()         // 从栈中获取次数
          result = strStack.pop() + result.repeat(repeatTimes)
        } else {
          result += item
        }
      }
      return result
    }
    console.log(fn('3[a]2[bc]'))
  </script>
</body>
</html>